// pages/study/study.js
Page({
  data: {
    globalData: getApp().globalData,
    showArt: true,
    articles: [],
    pageNum: 1,
    pageSize: 10,
    total: 0,
    isLoading: false
  },

  onLoad(options) {
    this.getArticles()
  },
  onReachBottom() {
    if(this.data.pageNum * this.data.pageSize >= this.data.total) {
      return wx.showToast({
        title: '没有更多了~',
        icon: 'none'
      })
    }
    if(this.data.isLoading) return  // 判断是否正在加载其他数据
    this.setData({
      pageNum: this.data.pageNum + 1
    })
    this.getArticles()
  },
  onPullDownRefresh() {
    this.setData({
      pageNum: 1,
      articles: [],
      total: 0
    })
    this.getArticles()
  },

  getArticles() {
    this.setData({isLoading: true})
    wx.showLoading({
      title: '数据加载中...',
    })
    var that = this
    wx.request({
      url: this.data.globalData.server + '/article/query',
      method: 'GET',
      data: {pageNum: this.data.pageNum, pageSize: this.data.pageSize},
      success(res) {
        that.setData({
          articles: [...that.data.articles, ...res.data.data.records],
          total: res.data.data.total
        })
      },
      complete() {
        wx.hideLoading()
        that.setData({isLoading: false})
      }
    })
  },
  toArticleDetail(event) {
    let id = event.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/studyDetail/studyDetail?id=' + id,
    })
  },
  switch() {
    this.setData({showArt: !this.data.showArt})
  },

})